(function($) {

	var messageTable = $("#messageTable").dataTable({
		"destroy" : true, // 初始化表格
		"searching" : false,// 搜索框
		"deferRender" : true,
		"processing" : true,
		"serverSide" : true,
		"aLengthMenu" : [ 10, 20, 30, 50, 100, 500 ], // 更改显示记录数选项
		"ajax" : {
			// url可以直接指定远程的json文件，或是MVC的请求地址 /Controller/Action
			url : base_url + "/message/pageList",
			type : 'POST',
			// 传给服务器的数据，可以添加我们自己的查询参数
			data : function(param) {
				var obj = {};
				obj.employeeId = $("#employeeId").val();
				obj.type = $("#type").val();
				obj.pushChannel = $("#pushChannel").val();
				obj.start = param.start;
				obj.length = param.length;
				return obj;
			}
		},
		"language" : {
			"sProcessing" : "处理中...",
			"sLengthMenu" : "每页 _MENU_ 条记录",
			"sZeroRecords" : "没有匹配结果",
			"sInfo" : "第 _PAGE_ 页 ( 总共 _PAGES_ 页，_TOTAL_ 条记录 )",
			"sInfoEmpty" : "无记录",
			"sInfoFiltered" : "(由 _MAX_ 项结果过滤)",
			"sInfoPostFix" : "",
			"sSearch" : "搜索",
			"sUrl" : "",
			"sEmptyTable" : "表中数据为空",
			"sLoadingRecords" : "载入中...",
			"sInfoThousands" : ",",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			},
			"oAria" : {
				"sSortAscending" : ": 以升序排列此列",
				"sSortDescending" : ": 以降序排列此列"
			}
		},
		"columns" : [ {
			"data" : 'id',
			"bSortable" : false,
			"visible" : false
		}, {
			"title" : '消息类型',
			"data" : 'type',
			"bSortable" : false,
			"visible" : true,
			"width" : '10%',
			"render" : function(data, type, row, meta) {
				
				var msgType = parseInt(data/ 10);
				switch(msgType){
				case 1 :return '考勤';
				case 2 :return '审批';
				case 3 :return '流水';
				case 4 :return '会议室';
				case 5 :return '工单';
				case 6 :return '帖子';
				case 7 :return '系统通知';
				case 8 :return '尚德大学';
				case 9 :return '爱尚家';
				default:return data;
				}
			}
		}, {
			"title" : '应用名称',
			"data" : 'name',
			"bSortable" : false,
			"visible" : true,
			"width" : '5%'
		}, {
			"title" : 'logo',
			"data" : 'logoUrl',
			"bSortable" : false,
			"visible" : true,
			"width" : '5%',
			"render" : function(data, type, row, meta) {
				if(""==data){
					return data;
				}
				return '<img src="' + data + '" style="width:30px;height:30px;"/>';
			}
		}, {
			"title" : '消息标题',
			"data" : 'title',
			"bSortable" : false,
			"visible" : true,
			"width" : '10%'
		}, {
			"title" : '消息内容',
			"data" : 'content',
			"bSortable" : false,
			"visible" : true,
			"width" : '10%'
		}, {
			"title" : '状态',
			"data" : 'isRead',
			"bSortable" : false,
			"visible" : true,
			"width" : '5%',
			"render" : function(data, type, row, meta) {
				return 0 == data ? "未读" : "已读";
			}
		} , {
			"title" : '链接',
			"data" : 'link',
			"bSortable" : false,
			"visible" : true,
			"width" : '10%'
		}, {
			"title":'员工ID',
			"data" : 'employeeId',
			"bSortable" : false,
			"visible" : true,
			"width" : '10%'
		}, {
			"title" : '推送渠道',
			"data" : 'pushChannel',
			"bSortable" : false,
			"visible" : true,
			"width" : '5%',
			"render" : function(data, type, row, meta) {
				switch(data){
				case -1 :return '未知渠道';
				case 0 :return '信鸽';
				case 1 :return '小米';
				case 2 :return 'IM';
				
				}
			}
		}
		, {
			"title" : '推送时间',
			"data" : 'pushTime',
			"bSortable" : false,
			"visible" : true,
			"width" : '10%',
			"render" : function(data, type, row, meta) {
				return formatDateTime(data);
			}
		}
		, {
			"title" : '额外信息',
			"data" : 'extra',
			"bSortable" : false,
			"visible" : true,
			"width" : '10%'
		}
		]

	});

	$('#searchBtn').on('click', function() {
		messageTable.fnDraw();
	});
	
	
	function formatDateTime(inputTime) {  
	    var date = new Date(inputTime);
	    var y = date.getFullYear();  
	    var m = date.getMonth() + 1;  
	    m = m < 10 ? ('0' + m) : m;  
	    var d = date.getDate();  
	    d = d < 10 ? ('0' + d) : d;  
	    var h = date.getHours();
	    h = h < 10 ? ('0' + h) : h;
	    var minute = date.getMinutes();
	    var second = date.getSeconds();
	    minute = minute < 10 ? ('0' + minute) : minute;  
	    second = second < 10 ? ('0' + second) : second; 
	    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;  
	}

})(jQuery);